﻿package com.winmastergames.gameassets {
	import flash.display.*;
	import flash.events.*;
	import flash.utils.Dictionary;
	import flash.utils.Timer;
	import flash.net.SharedObject;
	import com.winmastergames.gameassets.achievement;

	public class achievements extends MovieClip {
		public static var sharedObject:SharedObject;
		//public static var killsAchievement:Number = 0;
		//public var killsAchievementComplete:Boolean;
		//public static var pistolAchievement:Number = 0;
		//public var pistolAchievementComplete:Boolean;
		public var visibleOff:Timer = new Timer(2000);
		public var noticeBusy:Boolean = false;
		public var noticeFinished:Boolean = false;
		public var lastVar:String = "0";
		var achievementsArray:Dictionary = new Dictionary();
		public static var st_achievementsArray:Dictionary = new Dictionary();

		public function achievements(){
			sharedObject = SharedObject.getLocal("test");
			visible = false;
			addEventListener(Event.ENTER_FRAME, loop);
			visible = true;
			visibleOff.addEventListener(TimerEvent.TIMER, makeInvisible);
			loadAchievements();
			setUpAchievements();
			st_achievementsArray = achievementsArray;
		}

		public function loadAchievements(){
			//sharedObject.data.killsAchievement = 0;
			//killsAchievement = sharedObject.data.killsAchievement;
			//sharedObject.data.pistolAchievement = 0;
			//pistolAchievement = sharedObject.data.killsAchievement;
		}

		public function makeInvisible(e:Event){
			visible = false;
			noticeBusy = false;
			visibleOff.stop();
			visibleOff.reset();
			noticeFinished = true;
		}
		
		public function setUpAchievements()
		{
			achievementsArray["pistolKills"] = new achievement("Pistol Kills","pistolKills", 75, 25);
			achievementsArray["monsterKills"] = new achievement("Monster Kills", "monsterKills", 150, 50);
		}

		public static function setAchievements(aName:String, amount:Number){
			sharedObject.data.aName = amount;
		}

		public function loop(e:Event){
			checkAchievements();
		}

		public function checkAchievements() {
			for each(var tempAchievements:achievement in achievementsArray)
			{
				tempAchievements.save();
				if (tempAchievements.gotten == false)
				{
					if (tempAchievements.total % tempAchievements.frequency == 0 && tempAchievements.poppedUp == false)
					{
						achievementNotice("notice", tempAchievements.name, tempAchievements.total.toString(), tempAchievements.required.toString());
						tempAchievements.poppedUp = true;
					}
					else if (tempAchievements.total >= tempAchievements.required)
						tempAchievements.gotten = true;
				}
			}
		}
		
		
		public function achievementNotice(type:String, aName:String, aProgress:String, aTotal:String){
			if (type == "finished"){
				lastVar = aProgress;
				visibleOff.start();
				visible = true;
				aProgresss.text = aProgress;
				aTotall.text = aTotal;
				body.text = "Congrats on getting the\n" + aName + " Achievement!";
			} else if (type == "notice"){
				lastVar = aProgress;
				visibleOff.start();
				visible = true;
				aProgresss.text = aProgress;
				aTotall.text = aTotal;
				body.text = "Progress on the \n" + aName + "Achievement";
			}
		}
	}
}